package de.justphil.tcg.tcgserver.commons.domain;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import de.justphil.tcg.tcgserver.commons.util.DateTimeUtil;

@Entity
@Table(name = "decks", schema = "tcg")
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Deck implements Serializable {

	//private static final Logger log = restLogger(Deck.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -1181629336234214376L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "deck_id")
	@XmlElement
	private long id;
	
	@Column(name = "name")
	@XmlElement
	private String name;
	
	// foreign key mapping the owning user
	@Column(name = "user_id")
    private long userId;
	
	@JoinTable(name = "deck_card_instances", joinColumns = {
	        @JoinColumn(name = "deck_id", referencedColumnName = "deck_id")}, inverseJoinColumns = {
	        @JoinColumn(name = "card_instance_id", referencedColumnName = "card_instance_id")})
    @ManyToMany(fetch=FetchType.EAGER)
    @XmlElement
    private Set<CardInstance> cardInstances;
	
	@Column(name = "created")
	@Temporal(TemporalType.TIMESTAMP)
	private Date created;
	
	@Column(name = "modified")
	@Temporal(TemporalType.TIMESTAMP)
	private Date modified;
	
	
	

	public Deck() {
		// JPA requires a default constructor without parameters.
		// The DataSerializable interface as well.
		
		this.cardInstances = new HashSet<CardInstance>();
	}
	
	public Deck(String name, long userId) {
		this();
		
		this.name = name;
		this.userId = userId;
		
		long nowUTC = DateTimeUtil.getUTCTimeInMillis();
		this.created = new Date(nowUTC);
		this.modified = new Date(nowUTC);
	}

	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */

	public long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setId(long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public long getUserId() {
		return userId;
	}

	public void setUserId(long userId) {
		this.userId = userId;
	}

	public Set<CardInstance> getCardInstances() {
		return cardInstances;
	}

	public void setCardInstances(Set<CardInstance> cardInstances) {
		this.cardInstances = cardInstances;
	}

	public Date getCreated() {
		return created;
	}

	public Date getModified() {
		return modified;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public void setModified(Date modified) {
		this.modified = modified;
	}
	
	/* ############################################################################################ */
	/* ############################################################################################ */
	/* ############################################################################################ */

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (id ^ (id >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Deck other = (Deck) obj;
		if (id != other.id)
			return false;
		return true;
	}
	
	/* ######################################################################## */
	/* ######################################################################## */
	/* ######################################################################## */
	
	public long[] getCardInstanceIdsAsArray() {
		long[] out = new long[cardInstances.size()];
		
		int i = 0;
		for(CardInstance ci : cardInstances) {
			out[i] = ci.getId();
			
			i++;
		}
		
		return out;
	}
	
	/* ######################################################################## */
	/* ######################################################################## */
	/* ######################################################################## */

	/*
	@Override
	public void readData(DataInput in) throws IOException {
		id = in.readLong();
		name = in.readUTF();
		created = new Date(in.readLong());
		modified = new Date(in.readLong());
		
		user = new User();
		user.readData(in);
		
		
		int ciSize = in.readInt();
		cardInstances = new ArrayList<CardInstance>(ciSize);
		CardInstance ci = null;
		for(int i = 0; i < ciSize; i++) {
			ci = new CardInstance();
			ci.readData(in);
			cardInstances.add( ci );
		}
	}

	@Override
	public void writeData(DataOutput out) throws IOException {
		out.writeLong(id);
		out.writeUTF(name);
		out.writeLong(created.getTime());
		out.writeLong(modified.getTime());
		
		user.writeData(out);
		
		out.writeInt(cardInstances.size());
		for(CardInstance ci : cardInstances) {
			ci.writeData(out);
		}
	}
	*/

	

	public static class Name {
		public static final String SURVIVAL_OF_THE_FITTEST 	= "Survival of the Fittest";
		public static final String RENAISSANCE 				= "Renaissance";
		
		private static final List<String> starterDecks;
		private static final Map<String, String> starterDeckCards;
		
		static {
			starterDecks 		= new ArrayList<String>(2);
			starterDeckCards 	= new HashMap<String, String>();
			
			starterDecks.add(SURVIVAL_OF_THE_FITTEST);
			starterDecks.add(RENAISSANCE);
			
			starterDeckCards.put(Card.Name.BIGFOOT, 	SURVIVAL_OF_THE_FITTEST);
			starterDeckCards.put(Card.Name.BULLFROG, 	SURVIVAL_OF_THE_FITTEST);
			starterDeckCards.put(Card.Name.CERBERUS, 	SURVIVAL_OF_THE_FITTEST);
			starterDeckCards.put(Card.Name.ELEPHANT, 	SURVIVAL_OF_THE_FITTEST);
			starterDeckCards.put(Card.Name.ELF_MAGE, 	SURVIVAL_OF_THE_FITTEST);
			starterDeckCards.put(Card.Name.GOBLIN, 		SURVIVAL_OF_THE_FITTEST);
			
			starterDeckCards.put(Card.Name.HEDGEHOG, 	RENAISSANCE);
			starterDeckCards.put(Card.Name.MAGIC_ROCK, 	RENAISSANCE);
			starterDeckCards.put(Card.Name.ROADRUNNER, 	RENAISSANCE);
			starterDeckCards.put(Card.Name.SNAKE, 		RENAISSANCE);
			starterDeckCards.put(Card.Name.WILD_BORE, 	RENAISSANCE);
			starterDeckCards.put(Card.Name.WOLF, 		RENAISSANCE);
		}
		
		public static List<String> getStarterDecks() {
			return starterDecks;
		}
		
		public static String getStarterDeckByCardName(String cardName) {
			return starterDeckCards.get(cardName);
		}
	}
}
